'''
dionysus - a web based dialogue management framework
Copyright (C) 2011  Eugeniu Costetchi <eugeniu.costetchi (at) tudor.lu>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Created on Jan 3, 2011
'''
from DialogueManager.Parser import InputTypes
from DialogueManager.protocols.handlers import build_qaf_instance
import networkx
from matplotlib import pyplot
from DialogueManager.planning.Action import *
from DialogueManager.planning.Operator import *
from DialogueManager.protocols.sorting import sort
from DialogueManager.protocols.literals import *
from DialogueManager.planning.PlanFragment import PlanFragment
    
#if __name__ == '__main__':
#    pass

def greeting():
    return PlanFragment().add_sequence([SayInteractionAction('Hello, what is your name?'),
                          ListenInteractionAnction('@ctx.variables.' + NAME),
                          SayTemplateInteractionAction('I am glad to meet you ${' + NAME + '}')])

def discover_domain():
    return PlanFragment().add_sequence([SayTemplateInteractionAction('I would like to get to know what do you \
                                                    think of a particular topic.\n In the end of \
                                                    discussion I can present you the snapshot of your \
                                                    mental image on this particular topic. \n Are you ready? \
                                                    Let\'s go!'),
                          SayInteractionAction('Please tell me what would be a interesting \
                                                    topic of discussion for you?'),
                          ListenInteractionAnction('@ctx.variables.' + DOMAIN_TOPIC),
                                        ])
    
def free_listing():
    q = PlanFragment()
    q += SayTemplateInteractionAction('Now, tell me what are the words that first come \
                                            to your mind when you think of *${domain_topic}*.')
    q += ListenInteractionAnction()
    q += CopyVariable('@ctx.last_user_utterance.analysed_tokens',
                                                             '@ctx.variables.' + FREE_LISTING, False)
    
    condition = Not(Or(And(LessThan(Length('@ctx.last_user_utterance.analysed_tokens'), 14),
                      GreaterThan(Length('@ctx.last_user_utterance.analysed_tokens'), 7)),
                    IsInputType(InputTypes.NONE)))

    while_sequence = PlanFragment() + SayInteractionAction('The initial list of words is recommended \
                                 to be between 7 and 14 words long. The duration and meaningfulness of the interview is partially determined by \
                                 this list')
    while_sequence += ListenInteractionAnction()
    while_sequence = while_sequence.add_condition(Not(IsInputType(InputTypes.NONE)),
                                 PlanFragment() + CopyVariable('@ctx.last_user_utterance.analysed_tokens',
                                                             '@ctx.variables.' + FREE_LISTING, False))
    return q.add_while(condition, while_sequence)
    
def word_by_word():
    factory = PlanFragment() + SayTemplateInteractionAction('At this stage of our discussion I already \
                                                    know what is important for you when you talk about \
                                                    ${domain_topic}.\n Now I would like to talk one by one, \
                                                    about most important words that you outlined.')
    
    p = PlanFragment() + build_qaf_instance('hyponimy')
    p += build_qaf_instance('hypernimy')
    p += build_qaf_instance('meronimy')
    p += build_qaf_instance('holonimy')
    return factory.add_for_each('@ctx.variables.word', '@ctx.variables.' + FREE_LISTING, p)

def all_fragments():
    return greeting() + discover_domain() + free_listing() + sort() + word_by_word() + FormalizeGroupsToTurtleTriples() + DumpContext()

def test_if():
    #condition = QafExpectedAnswer('@ctx.qaf_instances')
#    condition = Not(And(
#                    And(
#                       , 
#                       GreaterThan(Length('@ctx.last_user_utterance.analysed_tokens'),7)), 
#                    Not(IsInputType(InputTypes.NONE))
#                    ))
    condition = Not(Or(And(LessThan(Length('@ctx.last_user_utterance.analysed_tokens'), 14),
                      GreaterThan(Length('@ctx.last_user_utterance.analysed_tokens'), 7)),
                    IsInputType(InputTypes.NONE)))
    p = PlanFragment().add_sequence([SayInteractionAction('hello'), ListenInteractionAnction()]).add_condition(condition,
                                                                                      PlanFragment().add_action(SayInteractionAction('Then Branch')),
                                                                                      PlanFragment().add_action(SayInteractionAction('Else Branch')))
    return PlanFragment().add_while(Eq(True, True), p)
#

#print  issub isinstance('', operand_type(''))

